<template>
  <div class="worktask-designer-container">
    <!-- <top-tip :tipcontent="tipcontent" /> -->
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <el-form-item>
              <el-input
                v-model.trim="queryForm.OrderCode"
                clearable
                placeholder="按合同编号"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="按客户名称"
                @input="trimkey"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.storeAccount"
                clearable
                placeholder="按店铺账号"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.assignStaff"
                clearable
                placeholder="按派单人"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane label="新任务待接单" name="new">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="任务ID"
            prop="WorkTaskID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同ID"
            prop="BusinessOrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="业务产品"
            prop="ProductName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="派单人"
            prop="AssignStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="派单时间"
            prop="AddTime"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="任务接单人"
            prop="TaskStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="店铺/账号"
            prop="BindStoreAccount"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="操作" width="100">
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="onOrder(row)">
                接单
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="查看任务并处理" width="200">
            <template #default="{ row }">
              <el-button size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已接单制作中" name="tasking">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="任务ID"
            prop="WorkTaskID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同ID"
            prop="BusinessOrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="业务产品"
            prop="ProductName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="派单人"
            prop="AssignStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="派单时间"
            prop="AddTime"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="任务接单人"
            prop="TaskStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="店铺/账号"
            prop="BindStoreAccount"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="查看任务并处理" width="200">
            <template #default="{ row }">
              <el-button size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已完成/已提交/待审核" name="finished">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="任务ID"
            prop="WorkTaskID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同ID"
            prop="BusinessOrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="业务产品"
            prop="ProductName"
            show-overflow-tooltip
          />

          <el-table-column
            align="center"
            label="派单人"
            prop="AssignStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="派单时间"
            prop="AddTime"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="任务接单人"
            prop="TaskStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="店铺/账号"
            prop="BindStoreAccount"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="查看任务并处理" width="200">
            <template #default="{ row }">
              <el-button size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="质量审核已通过" name="taskend">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="任务ID"
            prop="WorkTaskID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同ID"
            prop="BusinessOrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="业务产品"
            prop="ProductName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="派单人"
            prop="AssignStaffName"
            show-overflow-tooltip
            width="100"
          />
          <el-table-column align="center" label="派单时间" width="150">
            <template #default="{ row }">
              {{ row.AddTime.split(' ')[0] }}
              <br />
              {{ row.AddTime.split(' ')[1] }}
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            label="任务接单人"
            prop="TaskStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="店铺/账号"
            prop="BindStoreAccount"
            show-overflow-tooltip
          /> -->
          <el-table-column align="center" label="任务清单" width="450">
            <template #default="{ row }">
              <table style="width: 100%">
                <tr v-for="(item, index) in row.WorkTaskList" :key="index">
                  <td
                    style="
                      width: 100px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                      text-align: left;
                    "
                  >
                    {{ item.WorkTaskName }}
                  </td>
                  <td
                    align="right"
                    style="
                      width: 10px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                      color: #ff6600;
                    "
                  >
                    {{ item.FinishNum }}
                  </td>
                  <td
                    align="center"
                    style="
                      width: 80px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                    "
                  >
                    {{ item.FinishDate }}
                  </td>
                  <td
                    align="right"
                    style="
                      width: 30px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                      color: green;
                    "
                  >
                    {{ item.FinishTichengMoney }}
                  </td>
                  <td
                    align="center"
                    style="
                      width: 100px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                    "
                  >
                    {{ item.SubmitCheckTime }}
                  </td>
                </tr>
              </table>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="查看任务并处理"
            width="200"
          >
            <template #default="{ row }">
              <el-button size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="质量审核未通过" name="reject">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="任务ID"
            prop="WorkTaskID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同ID"
            prop="BusinessOrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="业务产品"
            prop="ProductName"
            show-overflow-tooltip
          />

          <el-table-column
            align="center"
            label="派单人"
            prop="AssignStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="派单时间"
            prop="AddTime"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="任务接单人"
            prop="TaskStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="店铺/账号"
            prop="BindStoreAccount"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="任务清单" width="450">
            <template #default="{ row }">
              <table style="width: 100%">
                <tr v-for="(item, index) in row.WorkTaskList" :key="index">
                  <td
                    style="
                      width: 100px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                      text-align: left;
                    "
                  >
                    {{ item.WorkTaskName }}
                  </td>
                  <td
                    align="right"
                    style="
                      width: 10px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                      color: #ff6600;
                    "
                  >
                    {{ item.FinishNum }}
                  </td>
                  <td
                    align="center"
                    style="
                      width: 80px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                    "
                  >
                    {{ item.FinishDate }}
                  </td>
                  <td
                    align="right"
                    style="
                      width: 30px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                      color: green;
                    "
                  >
                    {{ item.FinishTichengMoney }}
                  </td>
                  <td
                    align="center"
                    style="
                      width: 100px;
                      padding: 3px;
                      border-bottom: 1px solid #f0f0f0;
                    "
                  >
                    {{ item.SubmitCheckTime }}
                  </td>
                </tr>
              </table>
            </template>
          </el-table-column>
          <el-table-column align="center" label="查看任务并处理" width="200">
            <template #default="{ row }">
              <el-button size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
  // import DateSelect from '/src/components/DateSelect.vue'
  import { getList, doDelete, takeWorkOrder } from '@/api/worktask'

  // import TopTip from '@/components/TopTip'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'DeveloperTask',
    // components: { DateSelect },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        DateSelectRef: null,
        tipcontent: {
          title: '开发类工作任务',
          memo:
            '本页用于“开发工作人员”接收所被分配的工作任务；默认显示尚未接受的美工任务，收到后先进行接受，进入到处理中，并相应进行任务的跟进记录；' +
            '处理完成后，提交相关信息，等待管理员审核。',
        },
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          AssignOrderSetID: 10,
          tab: 'new',
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })
      const trimkey = (val) => {
        state.queryForm.searchKey = val.replace(
          /[^\u4e00-\u9fa5a-zA-Z0-9]/g,
          ''
        )
      }
      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const getTackinfo = (row) => {
        router.push({
          path: 'developertaskinfo',
          query: {
            OrderID: row.BusinessOrderID,
            isTaskState: row.TaskState,
            WorkTaskID: row.WorkTaskID,
          },
        })
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }

      // 接单
      const onOrder = async (row) => {
        const dataMsg = {
          WorkTaskID: row.WorkTaskID,
        }
        const { msg } = await takeWorkOrder(dataMsg)
        state.queryForm.tab = 'tasking'
        $baseMessage(msg, 'success', 'vab-hey-message-success')
        fetchData()
        console.log(row, '点击的接单')
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      const getDate = (data) => {
        state.queryForm.orderDate = data
        console.log(data, '子组件传过来的时间数组')
      }

      return {
        ...toRefs(state),
        handleTabClick,
        setSelectRows,
        getDate,
        getTackinfo,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        onOrder,
        fetchData,
        Delete,
        Plus,
        Search,
        trimkey,
      }
    },
  })
</script>
